<?php
/**
 * SPRPC_CORE class
 * simply provides some tools for starters..
 * @static
 * @author bbqq
 * @copyright 2010 qstufie.com
 * @package com.sprpc.core
 */
// lib dir...
define('LIBDIR', dirname(__file__) . '/');

// no espace for exceptions! just in case if someone forgott to catch one exception...
set_exception_handler('SPRPC_Core::e_dump');

// use util...
SPRPC_Core::import('utility/config');
SPRPC_Core::import('utility/registry');
SPRPC_Core::import('utility/httprequest');
SPRPC_Core::import('utility/xml');
/**
 * core class
 * imports all
 * and prepare all
 */
class SPRPC_Core
{
    /**
     * stop instantiating
     * @return return false
     */
    private function __construct()
    {
        return false;

    }// end __construct


    // make sure no double require happens
    static protected $_imports = array();


    /**
     * import function
     * @path the path to the file
     * @once if true, only once
     * @return void
     */
    static public function import($path, $once = true)
    {
        // check if it's in holder already...
        if ($once) {
            $_key = md5($path);
            if (isset(self::$_imports[$_key])) {
                return;
            }
        }

        // interprete path
        $path = strtolower($path);

        // now let's see if it's core component
        if (substr($path, 0, 6) == 'sprpc_' && strpos($path, '/') === false) {
            // it's core!
            $path = substr($path, 6, strlen($path) - 6);
            $path = str_replace('_', '/', $path);
        }

        $path .= '.php';

        $path = LIBDIR . $path;

        // test if exists...
        if (!file_exists($path)) {
            throw new Exception('SPRPC_Core::import(' . $path . ') - File doesn\'t exist.');
        }

        if ($once) {
            self::$_imports[$_key] = $path;
            require_once $path;
            return;
        }

        // otherwise, just include it as many times as possible!
        include $path;

    }// end import


    /**
     * a way to force a singleton out of a class
     * NOTE: only class with no params in the constructor function
     * may be created this way.
     * @param str $className
     * @return one single instance of the object
     */
    static public function getSingleton($className)
    {
        // first test if exists...
        if (class_exists($className)) {
            self::import($className);
        }
        $key = '_SINGLETONE_' . $className;
        // test if registered...
        $obj = SPRPC_Utility_Registry::get($key);

        if ($obj instanceof $className) {
            return $obj;
        }

        // otherwise... set it up...
        $obj = new $className;
        SPRPC_Utility_Registry::set($key, $obj);
        unset($obj); // kill the original...
        return SPRPC_Utility_Registry::get($key);

    }// end getSingleton


    /**
     * dump the var...
     */
    static public function dump()
    {
        echo '<pre>';
        $vars = func_get_args();
        foreach ($vars as $var) {
            var_dump($var);
        }
        echo '</pre>';

    }// end dump


    /**
     * want a nice exception dumper?
     * here it is...
     */
    static public function e_dump(Exception $e)
    {
        // use a nice good looking table to do it...
        // let's do it..
        $tmp  = '<h1 style="border:none;background:none;padding:3px 12px;font:bold 22px arial;margin:0;">UNCAUGHT EXCEPTION:</h1>' .
                '<table style="background:#333;font:13px lucida sans;line-height:1.5;border:12px solid #555;border-radius: 12px;' .
                '-moz-border-radius: 12px;-webkit-border-radius: 12px;" cellspacing="1">' .
                '<tr>' .
                '<th style="color:#fff;text-align:left;padding:6px 10px;background:#999;">code</th>' .
                '<td style="color:#000;text-align:left;padding:6px 10px;background:#eee;">' . $e->getCode() . '</td>' .
                '</tr>' .
                '<tr>' .
                '<th style="color:#fff;text-align:left;padding:6px 10px;background:#999;">message</th>' .
                '<td style="color:#000;text-align:left;padding:6px 10px;background:#eee;font-weight:bold;"><pre>' . $e->getMessage() . '</pre></td>' .
                '</tr>' .
                '<tr>' .
                '<th style="color:#fff;text-align:left;padding:6px 10px;background:#999;">file</th>' .
                '<td style="color:#000;text-align:left;padding:6px 10px;background:#eee;">' . $e->getFile() . '</td>' .
                '</tr>';

        // special exceptions...
        if ($e instanceof SPRPC_Exception_Client) {
            $tmp .= '<tr>' .
                    '<th style="color:#fff;text-align:left;padding:6px 10px;background:#999;">Request</th>' .
                    '<td style="color:#000;text-align:left;padding:6px 10px;background:#eee;">' . $e->getRequest(true) . '</td>' .
                    '</tr>' .
                    '<tr>' .
                    '<th style="color:#fff;text-align:left;padding:6px 10px;background:#999;">Response</th>' .
                    '<td style="color:#000;text-align:left;padding:6px 10px;background:#eee;">' . $e->getResponse(true) . '</td>' .
                    '</tr>';
        }

        $cnt = 0;
        foreach ($e->getTrace() as $trace) {
            $cnt ++;
            $tmp .= '<tr>' .
                '<th style="color:#fff;text-align:left;padding:6px 10px;vertical-align:top;background:#999;">trace: ' . $cnt . '</th>' .
                '<td style="color:#000;text-align:left;padding:6px 10px;background:#eee;">';

            if (isset($trace['file'])) {
                $tmp .= '<b style="width:80px;float:left;">file:</b>' . $trace['file'];
            }

            if (isset($trace['line'])) {
                $tmp .= '<br /><b style="width:80px;float:left;">line:</b> ' . $trace['line'];
            }

            if (isset($trace['function'])) {
                $tmp .= '<br /><b style="width:80px;float:left;">function:</b> ' . $trace['function'];
            }

            if (isset($trace['class'])) {
                $tmp .= '<br /><b style="width:80px;float:left;">class:</b> ' . $trace['class'];
            }

            if (isset($trace['class'])) {
                $tmp .= '<br /><b style="width:80px;float:left;">type:</b> ' . $trace['type'];
            }

            $tmp .= '<br /><b style="width:80px;float:left;">args:</b><br /> <pre>' . print_r($trace['args'], 1) .
                '</pre></td>' .
                '</tr>';
        }
        $tmp .= '</table>';

        echo $tmp;

    } // end e_dump
}
?>